


--use PolePosition

-- // checksum failed custom error
--exec sp_addmessage @msgnum = 505050, @severity = 16, @msgtext = N'%s(%s) checksum compare failed', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // not found custom error
--exec sp_addmessage @msgnum = 505060, @severity = 16, @msgtext = N'%s(%s) does not exist', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // unit test custom error
--exec sp_addmessage @msgnum = 505070, @severity = 16, @msgtext = N'%s Test Failed. Expected< %s > Actual< %s > %s', @replace = 'REPLACE', @lang = 'us_english'
GO



--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'v_tb_Utility_Asset_Meters') and OBJECTPROPERTY(id, N'IsProcedure') = 0)
drop view v_tb_Utility_Asset_Meters
GO

create view v_tb_Utility_Asset_Meters as
	/* 	Initial Generated Date:  Thursday, March 05, 2009 
		Purpose: Selected record(s) from the table; this view is the source for all procedures' select statements
	*/
	
select
	 [Utility_Asset_MeterId] as [Utility_Asset_MeterId]
	,[UtilityFK] as [UtilityFK]
	,[Latitude] as [Latitude]
	,[Longitude] as [Longitude]
	,[Altitude] as [Altitude]
	,[SatelliteCount] as [SatelliteCount]
	,[SatelliteTime] as [SatelliteTime]
	,[SolutionAvailable] as [SolutionAvailable]
	,[Handheld] as [Handheld]
	,[Meter_TopTypeFk] as [Meter_TopTypeFk]
	,[Utility_ContactFK] as [Utility_ContactFK]
	,[Date] as [Date]
	,[Utility_ProjectFK] as [Utility_ProjectFK]
	,[MapFk] as [MapFk]
	,[SectionFk] as [SectionFk]
	,[Number] as [Number]
	-- customization may be required for FriendlyName
	,Left(CONVERT(varchar(255), [Utility_Asset_MeterId]), 255) as [FriendlyName]
	,[Comments] as [Comments]
	,dbo.fn_DistributionMeter_GetNoteString([Utility_Asset_MeterId]) as [NotesDescription]	
from
	tb_Utility_Asset_Meters
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetMeter_Test') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetMeter_Test
GO


create proc up_UtilityAssetMeter_Test
	@Utility_Asset_MeterId int
as
	/* 	Initial Generated Date:  Thursday, March 05, 2009 
		Purpose: Test if a record exists, using the primary key
	*/

SET NOCOUNT ON	
	
if exists(
	select [Utility_Asset_MeterId] 
	from [tb_Utility_Asset_Meters] source
	where [source].[Utility_Asset_MeterId] = @Utility_Asset_MeterId)
begin
	return(1)
end
else
begin
	return(0)
end
GO






--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetMeter_Select') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetMeter_Select
GO

create proc up_UtilityAssetMeter_Select
	 @Utility_Asset_MeterId int = null 
	,@UtilityFK int = null 
	,@Latitude float = null 
	,@Longitude float = null 
	,@Altitude float = null 
	,@SatelliteCount int = null 
	,@SatelliteTime datetime = null 
	,@SolutionAvailable bit = null 
	,@Handheld nvarchar(100) = null 
	,@Meter_TopTypeFk int = null 
	,@Utility_ContactFK int = null 
	,@Date datetime = null 
	,@Utility_ProjectFK int = null 
	,@MapFk int = null 
	,@SectionFk int = null 
	,@Number varchar(25) = null 
as
	/* 	Initial Generated Date:  Thursday, March 05, 2009 
		Purpose: Selected record(s) based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

declare @table table
	(
	 Utility_Asset_MeterId int
	,UtilityFK int
	,Latitude float
	,Longitude float
	,Altitude float
	,SatelliteCount int
	,SatelliteTime datetime
	,SolutionAvailable bit
	,Handheld nvarchar(100)
	,Meter_TopTypeFk int
	,Utility_ContactFK int
	,Date datetime
	,Utility_ProjectFK int
	,MapFk int
	,SectionFk int
	,Number varchar(25)
	,[FriendlyName] varchar(255)
	)
	
insert @table
	(
	 [Utility_Asset_MeterId]
	,[UtilityFK]
	,[Latitude]
	,[Longitude]
	,[Altitude]
	,[SatelliteCount]
	,[SatelliteTime]
	,[SolutionAvailable]
	,[Handheld]
	,[Meter_TopTypeFk]
	,[Utility_ContactFK]
	,[Date]
	,[Utility_ProjectFK]
	,[MapFk]
	,[SectionFk]
	,[Number]
	,[FriendlyName]
	)
select 
	 [Utility_Asset_MeterId]
	,[UtilityFK]
	,[Latitude]
	,[Longitude]
	,[Altitude]
	,[SatelliteCount]
	,[SatelliteTime]
	,[SolutionAvailable]
	,[Handheld]
	,[Meter_TopTypeFk]
	,[Utility_ContactFK]
	,[Date]
	,[Utility_ProjectFK]
	,[MapFk]
	,[SectionFk]
	,[Number]
	,[FriendlyName]
from v_tb_Utility_Asset_Meters
where
		IsNull([v_tb_Utility_Asset_Meters].[Utility_Asset_MeterId], -1) = Coalesce(@Utility_Asset_MeterId, [v_tb_Utility_Asset_Meters].[Utility_Asset_MeterId], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[UtilityFK], -1) = Coalesce(@UtilityFK, [v_tb_Utility_Asset_Meters].[UtilityFK], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Latitude], -1) = Coalesce(@Latitude, [v_tb_Utility_Asset_Meters].[Latitude], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Longitude], -1) = Coalesce(@Longitude, [v_tb_Utility_Asset_Meters].[Longitude], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Altitude], -1) = Coalesce(@Altitude, [v_tb_Utility_Asset_Meters].[Altitude], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[SatelliteCount], -1) = Coalesce(@SatelliteCount, [v_tb_Utility_Asset_Meters].[SatelliteCount], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[SatelliteTime], '1/1/1900 12:00AM') = Coalesce(@SatelliteTime, [v_tb_Utility_Asset_Meters].[SatelliteTime], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Utility_Asset_Meters].[SolutionAvailable], 0) = Coalesce(@SolutionAvailable, [v_tb_Utility_Asset_Meters].[SolutionAvailable], 0)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Handheld], '') = Coalesce(@Handheld, [v_tb_Utility_Asset_Meters].[Handheld], '')
	AND	IsNull([v_tb_Utility_Asset_Meters].[Meter_TopTypeFk], -1) = Coalesce(@Meter_TopTypeFk, [v_tb_Utility_Asset_Meters].[Meter_TopTypeFk], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Utility_ContactFK], -1) = Coalesce(@Utility_ContactFK, [v_tb_Utility_Asset_Meters].[Utility_ContactFK], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Date], '1/1/1900 12:00AM') = Coalesce(@Date, [v_tb_Utility_Asset_Meters].[Date], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Utility_Asset_Meters].[Utility_ProjectFK], -1) = Coalesce(@Utility_ProjectFK, [v_tb_Utility_Asset_Meters].[Utility_ProjectFK], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[MapFk], -1) = Coalesce(@MapFk, [v_tb_Utility_Asset_Meters].[MapFk], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[SectionFk], -1) = Coalesce(@SectionFk, [v_tb_Utility_Asset_Meters].[SectionFk], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Number], '') = Coalesce(@Number, [v_tb_Utility_Asset_Meters].[Number], '')

-- primary select
select * from @table





	-- UtilityAssetMetersImageCollection select

select
	v_tb_Utility_Asset_Meters_Images.* 
from 
	v_tb_Utility_Asset_Meters_Images 
	JOIN @table x ON v_tb_Utility_Asset_Meters_Images.Utility_Asset_MeterFK = x.Utility_Asset_MeterId
	

	
GO 





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetMeter_Select_Top1') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetMeter_Select_Top1
GO

create proc up_UtilityAssetMeter_Select_Top1
	 @Utility_Asset_MeterId int = null 
	,@UtilityFK int = null 
	,@Latitude float = null 
	,@Longitude float = null 
	,@Altitude float = null 
	,@SatelliteCount int = null 
	,@SatelliteTime datetime = null 
	,@SolutionAvailable bit = null 
	,@Handheld nvarchar(100) = null 
	,@Meter_TopTypeFk int = null 
	,@Utility_ContactFK int = null 
	,@Date datetime = null 
	,@Utility_ProjectFK int = null 
	,@MapFk int = null 
	,@SectionFk int = null 
	,@Number varchar(25) = null 
as
	/* 	Initial Generated Date:  Thursday, March 05, 2009 
		Purpose: Selected record(s) based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

declare @table table
	(
	  Utility_Asset_MeterId int
	, UtilityFK int
	, Latitude float
	, Longitude float
	, Altitude float
	, SatelliteCount int
	, SatelliteTime datetime
	, SolutionAvailable bit
	, Handheld nvarchar(100)
	, Meter_TopTypeFk int
	, Utility_ContactFK int
	, Date datetime
	, Utility_ProjectFK int
	, MapFk int
	, SectionFk int
	, Number varchar(25)
	,[FriendlyName] varchar(255)
	)

insert @table
	(
	 [Utility_Asset_MeterId]
	,[UtilityFK]
	,[Latitude]
	,[Longitude]
	,[Altitude]
	,[SatelliteCount]
	,[SatelliteTime]
	,[SolutionAvailable]
	,[Handheld]
	,[Meter_TopTypeFk]
	,[Utility_ContactFK]
	,[Date]
	,[Utility_ProjectFK]
	,[MapFk]
	,[SectionFk]
	,[Number]
	,[FriendlyName]
	)
select TOP 1
	 [Utility_Asset_MeterId]
	,[UtilityFK]
	,[Latitude]
	,[Longitude]
	,[Altitude]
	,[SatelliteCount]
	,[SatelliteTime]
	,[SolutionAvailable]
	,[Handheld]
	,[Meter_TopTypeFk]
	,[Utility_ContactFK]
	,[Date]
	,[Utility_ProjectFK]
	,[MapFk]
	,[SectionFk]
	,[Number]
	,[FriendlyName]
from v_tb_Utility_Asset_Meters
where
		IsNull([v_tb_Utility_Asset_Meters].[Utility_Asset_MeterId], -1) = Coalesce(@Utility_Asset_MeterId, [v_tb_Utility_Asset_Meters].[Utility_Asset_MeterId], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[UtilityFK], -1) = Coalesce(@UtilityFK, [v_tb_Utility_Asset_Meters].[UtilityFK], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Latitude], -1) = Coalesce(@Latitude, [v_tb_Utility_Asset_Meters].[Latitude], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Longitude], -1) = Coalesce(@Longitude, [v_tb_Utility_Asset_Meters].[Longitude], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Altitude], -1) = Coalesce(@Altitude, [v_tb_Utility_Asset_Meters].[Altitude], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[SatelliteCount], -1) = Coalesce(@SatelliteCount, [v_tb_Utility_Asset_Meters].[SatelliteCount], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[SatelliteTime], '1/1/1900 12:00AM') = Coalesce(@SatelliteTime, [v_tb_Utility_Asset_Meters].[SatelliteTime], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Utility_Asset_Meters].[SolutionAvailable], 0) = Coalesce(@SolutionAvailable, [v_tb_Utility_Asset_Meters].[SolutionAvailable], 0)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Handheld], '') = Coalesce(@Handheld, [v_tb_Utility_Asset_Meters].[Handheld], '')
	AND	IsNull([v_tb_Utility_Asset_Meters].[Meter_TopTypeFk], -1) = Coalesce(@Meter_TopTypeFk, [v_tb_Utility_Asset_Meters].[Meter_TopTypeFk], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Utility_ContactFK], -1) = Coalesce(@Utility_ContactFK, [v_tb_Utility_Asset_Meters].[Utility_ContactFK], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Date], '1/1/1900 12:00AM') = Coalesce(@Date, [v_tb_Utility_Asset_Meters].[Date], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Utility_Asset_Meters].[Utility_ProjectFK], -1) = Coalesce(@Utility_ProjectFK, [v_tb_Utility_Asset_Meters].[Utility_ProjectFK], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[MapFk], -1) = Coalesce(@MapFk, [v_tb_Utility_Asset_Meters].[MapFk], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[SectionFk], -1) = Coalesce(@SectionFk, [v_tb_Utility_Asset_Meters].[SectionFk], -1)
	AND	IsNull([v_tb_Utility_Asset_Meters].[Number], '') = Coalesce(@Number, [v_tb_Utility_Asset_Meters].[Number], '')

-- primary select
select * from @table


	-- UtilityAssetMetersImageCollection select

select
	v_tb_Utility_Asset_Meters_Images.* 
from 
	v_tb_Utility_Asset_Meters_Images 
	JOIN @table x ON v_tb_Utility_Asset_Meters_Images.Utility_Asset_MeterFK = x.Utility_Asset_MeterId
	
	
	
GO 





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetMeter_Insert') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetMeter_Insert
GO

create proc up_UtilityAssetMeter_Insert
	 @Utility_Asset_MeterId int =null OUTPUT  
	,@UtilityFK int  
	,@Latitude float = null  
	,@Longitude float = null  
	,@Altitude float = null  
	,@SatelliteCount int = null  
	,@SatelliteTime datetime = null  
	,@SolutionAvailable bit  
	,@Handheld nvarchar(100) = null  
	,@Meter_TopTypeFk int = null  
	,@Utility_ContactFK int = null  
	,@Date datetime = null  
	,@Utility_ProjectFK int = null  
	,@MapFk int = null  
	,@SectionFk int = null  
	,@Number varchar(25) = null  
as
	/* 	Initial Generated Date:  Thursday, March 05, 2009 
		Purpose: Inserts a new record; missing arguments are set as their default (typically null)
	*/

SET NOCOUNT ON	
--int

insert tb_Utility_Asset_Meters 
	(
	
	 [UtilityFK] 
	,[Latitude] 
	,[Longitude] 
	,[Altitude] 
	,[SatelliteCount] 
	,[SatelliteTime] 
	,[SolutionAvailable] 
	,[Handheld] 
	,[Meter_TopTypeFk] 
	,[Utility_ContactFK] 
	,[Date] 
	,[Utility_ProjectFK] 
	,[MapFk] 
	,[SectionFk] 
	,[Number] 
	)
	values
	(
	
	 @UtilityFK 
	,@Latitude 
	,@Longitude 
	,@Altitude 
	,@SatelliteCount 
	,@SatelliteTime 
	,@SolutionAvailable 
	,@Handheld 
	,@Meter_TopTypeFk 
	,@Utility_ContactFK 
	,@Date 
	,@Utility_ProjectFK 
	,@MapFk 
	,@SectionFk 
	,@Number 
	)
set @Utility_Asset_MeterId = scope_identity()
select * from v_tb_Utility_Asset_Meters where Utility_Asset_MeterId = @Utility_Asset_MeterId

GO




--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetMeter_Update') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetMeter_Update
GO


create proc up_UtilityAssetMeter_Update
	 @Utility_Asset_MeterId int  
	,@UtilityFK int = null  
	,@Latitude float = null  
	,@Longitude float = null  
	,@Altitude float = null  
	,@SatelliteCount int = null  
	,@SatelliteTime datetime = null  
	,@SolutionAvailable bit = null  
	,@Handheld nvarchar(100) = null  
	,@Meter_TopTypeFk int = null  
	,@Utility_ContactFK int = null  
	,@Date datetime = null  
	,@Utility_ProjectFK int = null  
	,@MapFk int = null  
	,@SectionFk int = null  
	,@Number varchar(25) = null  
	,@OriginalUtility_Asset_MeterId int  = null --Original PrimaryKey
	
as
	/* 	Initial Generated Date:  Thursday, March 05, 2009 
		Purpose: Updates an existing record, verifying exist; missing arguments persist original record value
		NOTE: this version of the update allows up to write nulls to the table.
	*/

SET NOCOUNT ON	

if @OriginalUtility_Asset_MeterId is null
	set @OriginalUtility_Asset_MeterId = @Utility_Asset_MeterId

-- test
declare @Result bit
execute @Result = up_UtilityAssetMeter_Test @OriginalUtility_Asset_MeterId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @Utility_Asset_MeterId)
	raiserror(505060, 16, 1, 'UtilityAssetMeter', @x)
	return
end

-- proceed
update tb_Utility_Asset_Meters 
set

	 [UtilityFK] = IsNull(@UtilityFK, [source].[UtilityFK])
	,[Latitude] =  @Latitude
	,[Longitude] =  @Longitude
	,[Altitude] =  @Altitude
	,[SatelliteCount] =  @SatelliteCount
	,[SatelliteTime] =  @SatelliteTime
	,[SolutionAvailable] = IsNull(@SolutionAvailable, [source].[SolutionAvailable])
	,[Handheld] =  @Handheld
	,[Meter_TopTypeFk] =  @Meter_TopTypeFk
	,[Utility_ContactFK] =  @Utility_ContactFK
	,[Date] =  @Date
	,[Utility_ProjectFK] =  @Utility_ProjectFK
	,[MapFk] =  @MapFk
	,[SectionFk] =  @SectionFk
	,[Number] =  @Number
	
from tb_Utility_Asset_Meters source
where
	[source].[Utility_Asset_MeterId] = @OriginalUtility_Asset_MeterId


select * from v_tb_Utility_Asset_Meters where Utility_Asset_MeterId = @Utility_Asset_MeterId
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetMeter_Delete') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetMeter_Delete
GO


create proc up_UtilityAssetMeter_Delete
	@Utility_Asset_MeterId int
as
	/* 	Initial Generated Date:  Thursday, March 05, 2009 
		Purpose: Deletes an existing record, verifying exist
	*/

SET NOCOUNT ON	

-- test
declare @Result bit
execute @Result = up_UtilityAssetMeter_Test @Utility_Asset_MeterId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @Utility_Asset_MeterId)
	raiserror(505060, 16, 1, 'UtilityAssetMeter', @x)
	return
end

-- proceed
delete from tb_Utility_Asset_Meters 
where [Utility_Asset_MeterId] = @Utility_Asset_MeterId

GO


